<?php

require_once (fvSite::$fvConfig->get("path.entity") . 'customer.class.php') ;

class CustomerManager extends fvRootManager {
	
	protected function __construct () {
	    $objectClassName = substr(__CLASS__, 0, -7);
	    
	    $this->rootObj = new $objectClassName();
	}
	
    static function getInstance()
    {
        static $instance; 
        if (!isset($instance)) {
            $instance = new self();
        }  
        return $instance;
    }
    
    /**
     * Function execute login into system. Return instance of logged user in success, or false otherwise.
     * 
     * Function retrieve $password field as md5 hash of password. 
     *
     * @param String $login
     * @param String $password
     * @param String $site
     * @param String $ip
     * @return User
     */
    public function Login($login, $password) {       
        
        $query = "select * from " . $this->rootObj->getTableName() . " where email = ? and password = ?";// and active = 1
        $res = fvSite::$DB->query($query, array($login, $password));
        if (!DB::isError($res)) {
            if (!is_array($row = $res->fetchRow())) return false;
            $Customer = clone $this->rootObj;
            $Customer->hydrate($row);
        }
        
        if ($Customer instanceof Customer)
            return $Customer;
        else return false;
    }
    
	public function generatePassword($length, $allow = "abcdefghijklmnopqrstuvwxyz0123456789") {
		$i = 1;
		while ($i <= $length) {
			$max  = strlen($allow)-1;
			$num  = rand(0, $max);
			$temp = substr($allow, $num, 1);
			$ret  = $ret . $temp;
			$i++;
		}
		return $ret;
	}
}
